package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.myapplication.Fragment.BlankFragment;
import com.example.myapplication.Fragment.BlankFragment2;
import com.example.myapplication.Fragment.BlankFragment3;
import com.example.myapplication.Fragment.BlankFragment4;
import com.example.myapplication.Fragment.BlankFragment5;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    ViewPager2 viewPager;
    private LinearLayout llShou,llZhang,llJi,llTong,llMy;
    private ImageView ivShou,ivZhang,ivJi,ivTong,ivMy,ivCurrent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPager();
        initTableView();
    }
    private void initTableView(){
        llShou = findViewById(R.id.id_table_shou);
        llShou.setOnClickListener(this);
        llZhang = findViewById(R.id.id_table_zhang);
        llZhang.setOnClickListener(this);
        llJi = findViewById(R.id.id_table_ji);
        llJi.setOnClickListener(this);
        llTong = findViewById(R.id.id_table_tong);
        llTong.setOnClickListener(this);
        llMy = findViewById(R.id.id_table_my);
        llMy.setOnClickListener(this);

        ivShou = findViewById(R.id.table_vi_shou);
        ivZhang = findViewById(R.id.table_vi_zhang);
        ivJi = findViewById(R.id.table_vi_ji);
        ivTong = findViewById(R.id.table_vi_tong);
        ivMy = findViewById(R.id.table_vi_my);

        ivShou.setSelected(true);
        ivCurrent = ivShou;

    }


    private void initPager(){
        viewPager = findViewById(R.id.id_viewpager);
        ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(BlankFragment.newInstance("首页")) ;
        fragments.add(BlankFragment2.newInstance("账单ver"));
        fragments.add(BlankFragment3.newInstance("记一笔"));
        fragments.add(BlankFragment4.newInstance("统计"));
        fragments.add(BlankFragment5.newInstance("我的"));
        fragments.add(BlankFragment.newInstance("welcome"));
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),getLifecycle(),fragments);
        viewPager.setAdapter(pagerAdapter);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                changeTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }
    public void sss(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,FirstActivity.class);
        startActivity(intent);

    }
    public void baidu(View view){
        Intent intent = new Intent();
        intent.setClass(MainActivity.this,SuccessActivity.class);
        startActivity(intent);

    }
    private void changeTab(int position){
        ivCurrent.setSelected(false);
        switch (position){
            case R.id.id_table_shou:
                viewPager.setCurrentItem(0);
               // viewPager.Fragment
           case 0:
               ivShou.setSelected(true);
               ivCurrent = ivShou;
               break;
            case R.id.id_table_zhang:
                viewPager.setCurrentItem(1);
           case 1:
               ivZhang.setSelected(true);
               ivCurrent = ivZhang;
               break;
            case R.id.id_table_ji:
                viewPager.setCurrentItem(2);
           case 2:
               ivJi.setSelected(true);
               ivCurrent = ivJi;
               break;
            case R.id.id_table_tong:
                viewPager.setCurrentItem(3);
           case 3:
               ivTong.setSelected(true);
               ivCurrent = ivTong;
               break;
            case R.id.id_table_my:
                viewPager.setCurrentItem(4);
           case 4:
               ivMy.setSelected(true);
               ivCurrent = ivMy;
               break;
       }
    }

    @Override
    public void onClick(View view){
        changeTab(view.getId());
    }

}